﻿using System;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class InstallmentWarningList : BasePage<InstallmentWarningListViewModel>, IPermittedPage
    {

        /// <summary>
        /// Gets or sets a value indicating whether [attachment saved].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [attachment saved]; otherwise, <c>false</c>.
        /// </value>
        private bool attachmentSaved
        {
            get { return (bool)ViewState["attachmentSaved"]; }
            set { ViewState["attachmentSaved"] = value; }
        }

        /// <summary>
        /// Gets or sets the current warning ID.
        /// </summary>
        /// <value>
        /// The current warning ID.
        /// </value>
        private Guid currentWarningID
        {
            get { return ViewState["currentWarningID"].ToString().ToGuid(); }
            set { ViewState["currentWarningID"] = value; }
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Ext.IsAjaxRequest)
            {
                attachmentSaved = false;
                currentWarningID = Guid.Empty;

                storeWarnings.DataSource = this.ViewModel.GetLoansWarnings();
                storeWarnings.DataBind();
            }
        }

        /// <summary>
        /// BTNs the save attachment.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnSaveAttachment(object sender, AjaxEventArgs e)
        {
            RowSelectionModel rowSelection = gridWarnings.SelectionModel.Primary as RowSelectionModel;
            Guid InstallmentWarningID = new Guid(rowSelection.SelectedRecordID);
            this.ViewModel.SaveInstallmentWarningAttachment(InstallmentWarningID, InsertAttchmentsCtrl.GetInsertedIDs(), InsertAttchmentsCtrl.GetDeletedIDs());
            attachmentSaved = true;
        }

        /// <summary>
        /// Handles the SelectedChanged event of the gridWarning control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void gridWarning_SelectedChanged(object sender, AjaxEventArgs e)
        {

            RowSelectionModel rowSelection = gridWarnings.SelectionModel.Primary as RowSelectionModel;
            Guid InstallmentWarningID = new Guid(rowSelection.SelectedRecordID);

            //if (!attachmentSaved && !currentWarningID.Equals(InstallmentWarningID))
            //{
            //    InsertAttchmentsCtrl.ResetAndDelete();                
            //}
            //else
            //{
            //InsertAttchmentsCtrl.Reset();
            //}
            attachmentSaved = false;
            currentWarningID = InstallmentWarningID;
            var images = this.ViewModel.GetWarningAttachment(InstallmentWarningID);
            InsertAttchmentsCtrl.SetImages(images);
        }

        public string PermissionName
        {
            get { return Permissions.InstallmentWarningList.ToString(); }
        }
    }
}
